
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using Apps.IDAL.Flow;
using Apps.Models.Flow;
using Apps.IBLL.Flow;
namespace Apps.BLL.Flow
{
	public partial class Flow_FormBLL: Virtual_Flow_FormBLL,IFlow_FormBLL
	{
        

	}
	public class Virtual_Flow_FormBLL
	{
        [Dependency]
        public IFlow_FormRepository m_Rep { get; set; }

		public virtual List<Flow_FormModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<Flow_Form> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.Id.Contains(queryStr)
								|| a.Name.Contains(queryStr)
								|| a.Remark.Contains(queryStr)
								|| a.UsingDep.Contains(queryStr)
								|| a.TypeId.Contains(queryStr)
								
								
								|| a.HtmlForm.Contains(queryStr)
								
								|| a.ExternalURL.Contains(queryStr)
								|| a.AttrA.Contains(queryStr)
								|| a.AttrB.Contains(queryStr)
								|| a.AttrC.Contains(queryStr)
								|| a.AttrD.Contains(queryStr)
								|| a.AttrE.Contains(queryStr)
								|| a.AttrF.Contains(queryStr)
								|| a.AttrG.Contains(queryStr)
								|| a.AttrH.Contains(queryStr)
								|| a.AttrI.Contains(queryStr)
								|| a.AttrJ.Contains(queryStr)
								|| a.AttrK.Contains(queryStr)
								|| a.AttrL.Contains(queryStr)
								|| a.AttrM.Contains(queryStr)
								|| a.AttrN.Contains(queryStr)
								|| a.AttrO.Contains(queryStr)
								|| a.AttrP.Contains(queryStr)
								|| a.AttrQ.Contains(queryStr)
								|| a.AttrR.Contains(queryStr)
								|| a.AttrS.Contains(queryStr)
								|| a.AttrT.Contains(queryStr)
								|| a.AttrU.Contains(queryStr)
								|| a.AttrV.Contains(queryStr)
								|| a.AttrW.Contains(queryStr)
								|| a.AttrX.Contains(queryStr)
								|| a.AttrY.Contains(queryStr)
								|| a.AttrZ.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }


		public virtual List<Flow_FormModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<Flow_FormModel>();
		}

        public virtual List<Flow_FormModel> CreateModelList(ref IQueryable<Flow_Form> queryData)
        {

            List<Flow_FormModel> modelList = (from r in queryData
                                              select new Flow_FormModel
                                              {
													Id = r.Id,
													Name = r.Name,
													Remark = r.Remark,
													UsingDep = r.UsingDep,
													TypeId = r.TypeId,
													State = r.State,
													CreateTime = r.CreateTime,
													HtmlForm = r.HtmlForm,
													IsExternal = r.IsExternal,
													ExternalURL = r.ExternalURL,
													AttrA = r.AttrA,
													AttrB = r.AttrB,
													AttrC = r.AttrC,
													AttrD = r.AttrD,
													AttrE = r.AttrE,
													AttrF = r.AttrF,
													AttrG = r.AttrG,
													AttrH = r.AttrH,
													AttrI = r.AttrI,
													AttrJ = r.AttrJ,
													AttrK = r.AttrK,
													AttrL = r.AttrL,
													AttrM = r.AttrM,
													AttrN = r.AttrN,
													AttrO = r.AttrO,
													AttrP = r.AttrP,
													AttrQ = r.AttrQ,
													AttrR = r.AttrR,
													AttrS = r.AttrS,
													AttrT = r.AttrT,
													AttrU = r.AttrU,
													AttrV = r.AttrV,
													AttrW = r.AttrW,
													AttrX = r.AttrX,
													AttrY = r.AttrY,
													AttrZ = r.AttrZ,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, Flow_FormModel model)
        {
            try
            {
                Flow_Form entity = m_Rep.GetById(model.Id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new Flow_Form();
               				entity.Id = model.Id;
				entity.Name = model.Name;
				entity.Remark = model.Remark;
				entity.UsingDep = model.UsingDep;
				entity.TypeId = model.TypeId;
				entity.State = model.State;
				entity.CreateTime = model.CreateTime;
				entity.HtmlForm = model.HtmlForm;
				entity.IsExternal = model.IsExternal;
				entity.ExternalURL = model.ExternalURL;
				entity.AttrA = model.AttrA;
				entity.AttrB = model.AttrB;
				entity.AttrC = model.AttrC;
				entity.AttrD = model.AttrD;
				entity.AttrE = model.AttrE;
				entity.AttrF = model.AttrF;
				entity.AttrG = model.AttrG;
				entity.AttrH = model.AttrH;
				entity.AttrI = model.AttrI;
				entity.AttrJ = model.AttrJ;
				entity.AttrK = model.AttrK;
				entity.AttrL = model.AttrL;
				entity.AttrM = model.AttrM;
				entity.AttrN = model.AttrN;
				entity.AttrO = model.AttrO;
				entity.AttrP = model.AttrP;
				entity.AttrQ = model.AttrQ;
				entity.AttrR = model.AttrR;
				entity.AttrS = model.AttrS;
				entity.AttrT = model.AttrT;
				entity.AttrU = model.AttrU;
				entity.AttrV = model.AttrV;
				entity.AttrW = model.AttrW;
				entity.AttrX = model.AttrX;
				entity.AttrY = model.AttrY;
				entity.AttrZ = model.AttrZ;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, Flow_FormModel model)
        {
            try
            {
                Flow_Form entity = m_Rep.GetById(model.Id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.Id = model.Id;
				entity.Name = model.Name;
				entity.Remark = model.Remark;
				entity.UsingDep = model.UsingDep;
				entity.TypeId = model.TypeId;
				entity.State = model.State;
				entity.CreateTime = model.CreateTime;
				entity.HtmlForm = model.HtmlForm;
				entity.IsExternal = model.IsExternal;
				entity.ExternalURL = model.ExternalURL;
				entity.AttrA = model.AttrA;
				entity.AttrB = model.AttrB;
				entity.AttrC = model.AttrC;
				entity.AttrD = model.AttrD;
				entity.AttrE = model.AttrE;
				entity.AttrF = model.AttrF;
				entity.AttrG = model.AttrG;
				entity.AttrH = model.AttrH;
				entity.AttrI = model.AttrI;
				entity.AttrJ = model.AttrJ;
				entity.AttrK = model.AttrK;
				entity.AttrL = model.AttrL;
				entity.AttrM = model.AttrM;
				entity.AttrN = model.AttrN;
				entity.AttrO = model.AttrO;
				entity.AttrP = model.AttrP;
				entity.AttrQ = model.AttrQ;
				entity.AttrR = model.AttrR;
				entity.AttrS = model.AttrS;
				entity.AttrT = model.AttrT;
				entity.AttrU = model.AttrU;
				entity.AttrV = model.AttrV;
				entity.AttrW = model.AttrW;
				entity.AttrX = model.AttrX;
				entity.AttrY = model.AttrY;
				entity.AttrZ = model.AttrZ;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual Flow_FormModel GetById(object id)
        {
            if (IsExists(id))
            {
                Flow_Form entity = m_Rep.GetById(id);
                Flow_FormModel model = new Flow_FormModel();
                              				model.Id = entity.Id;
				model.Name = entity.Name;
				model.Remark = entity.Remark;
				model.UsingDep = entity.UsingDep;
				model.TypeId = entity.TypeId;
				model.State = entity.State;
				model.CreateTime = entity.CreateTime;
				model.HtmlForm = entity.HtmlForm;
				model.IsExternal = entity.IsExternal;
				model.ExternalURL = entity.ExternalURL;
				model.AttrA = entity.AttrA;
				model.AttrB = entity.AttrB;
				model.AttrC = entity.AttrC;
				model.AttrD = entity.AttrD;
				model.AttrE = entity.AttrE;
				model.AttrF = entity.AttrF;
				model.AttrG = entity.AttrG;
				model.AttrH = entity.AttrH;
				model.AttrI = entity.AttrI;
				model.AttrJ = entity.AttrJ;
				model.AttrK = entity.AttrK;
				model.AttrL = entity.AttrL;
				model.AttrM = entity.AttrM;
				model.AttrN = entity.AttrN;
				model.AttrO = entity.AttrO;
				model.AttrP = entity.AttrP;
				model.AttrQ = entity.AttrQ;
				model.AttrR = entity.AttrR;
				model.AttrS = entity.AttrS;
				model.AttrT = entity.AttrT;
				model.AttrU = entity.AttrU;
				model.AttrV = entity.AttrV;
				model.AttrW = entity.AttrW;
				model.AttrX = entity.AttrX;
				model.AttrY = entity.AttrY;
				model.AttrZ = entity.AttrZ;
                if (!string.IsNullOrEmpty(entity.UsingDep))
                { 
                    string[] arr = entity.UsingDep.Split(',');
                    var arrname = m_Rep.Context.SysStruct.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                    model.UsingDepName = string.Join(",", arrname.ToArray());
                }
                return model;
            }
            else
            {
                return null;
            }
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<Flow_FormModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<Flow_FormModel>(x => x.Name, "Name");
				 excelFile.AddMapping<Flow_FormModel>(x => x.Remark, "Remark");
				 excelFile.AddMapping<Flow_FormModel>(x => x.UsingDep, "UsingDep");
				 excelFile.AddMapping<Flow_FormModel>(x => x.TypeId, "TypeId");
				 excelFile.AddMapping<Flow_FormModel>(x => x.State, "State");
				 excelFile.AddMapping<Flow_FormModel>(x => x.CreateTime, "CreateTime");
				 excelFile.AddMapping<Flow_FormModel>(x => x.HtmlForm, "HtmlForm");
				 excelFile.AddMapping<Flow_FormModel>(x => x.IsExternal, "IsExternal");
				 excelFile.AddMapping<Flow_FormModel>(x => x.ExternalURL, "ExternalURL");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrA, "AttrA");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrB, "AttrB");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrC, "AttrC");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrD, "AttrD");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrE, "AttrE");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrF, "AttrF");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrG, "AttrG");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrH, "AttrH");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrI, "AttrI");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrJ, "AttrJ");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrK, "AttrK");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrL, "AttrL");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrM, "AttrM");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrN, "AttrN");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrO, "AttrO");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrP, "AttrP");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrQ, "AttrQ");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrR, "AttrR");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrS, "AttrS");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrT, "AttrT");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrU, "AttrU");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrV, "AttrV");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrW, "AttrW");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrX, "AttrX");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrY, "AttrY");
				 excelFile.AddMapping<Flow_FormModel>(x => x.AttrZ, "AttrZ");
 
            //SheetName
            var excelContent = excelFile.Worksheet<Flow_FormModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new Flow_FormModel();
						 				  entity.Id = row.Id;
				  entity.Name = row.Name;
				  entity.Remark = row.Remark;
				  entity.UsingDep = row.UsingDep;
				  entity.TypeId = row.TypeId;
				  entity.State = row.State;
				  entity.CreateTime = row.CreateTime;
				  entity.HtmlForm = row.HtmlForm;
				  entity.IsExternal = row.IsExternal;
				  entity.ExternalURL = row.ExternalURL;
				  entity.AttrA = row.AttrA;
				  entity.AttrB = row.AttrB;
				  entity.AttrC = row.AttrC;
				  entity.AttrD = row.AttrD;
				  entity.AttrE = row.AttrE;
				  entity.AttrF = row.AttrF;
				  entity.AttrG = row.AttrG;
				  entity.AttrH = row.AttrH;
				  entity.AttrI = row.AttrI;
				  entity.AttrJ = row.AttrJ;
				  entity.AttrK = row.AttrK;
				  entity.AttrL = row.AttrL;
				  entity.AttrM = row.AttrM;
				  entity.AttrN = row.AttrN;
				  entity.AttrO = row.AttrO;
				  entity.AttrP = row.AttrP;
				  entity.AttrQ = row.AttrQ;
				  entity.AttrR = row.AttrR;
				  entity.AttrS = row.AttrS;
				  entity.AttrT = row.AttrT;
				  entity.AttrU = row.AttrU;
				  entity.AttrV = row.AttrV;
				  entity.AttrW = row.AttrW;
				  entity.AttrX = row.AttrX;
				  entity.AttrY = row.AttrY;
				  entity.AttrZ = row.AttrZ;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<Flow_FormModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        Flow_Form entity = new Flow_Form();
                       						entity.Id = ResultHelper.NewId;
						entity.Name = model.Name;
						entity.Remark = model.Remark;
						entity.UsingDep = model.UsingDep;
						entity.TypeId = model.TypeId;
						entity.State = model.State;
						entity.CreateTime = ResultHelper.NowTime;
						entity.HtmlForm = model.HtmlForm;
						entity.IsExternal = model.IsExternal;
						entity.ExternalURL = model.ExternalURL;
						entity.AttrA = model.AttrA;
						entity.AttrB = model.AttrB;
						entity.AttrC = model.AttrC;
						entity.AttrD = model.AttrD;
						entity.AttrE = model.AttrE;
						entity.AttrF = model.AttrF;
						entity.AttrG = model.AttrG;
						entity.AttrH = model.AttrH;
						entity.AttrI = model.AttrI;
						entity.AttrJ = model.AttrJ;
						entity.AttrK = model.AttrK;
						entity.AttrL = model.AttrL;
						entity.AttrM = model.AttrM;
						entity.AttrN = model.AttrN;
						entity.AttrO = model.AttrO;
						entity.AttrP = model.AttrP;
						entity.AttrQ = model.AttrQ;
						entity.AttrR = model.AttrR;
						entity.AttrS = model.AttrS;
						entity.AttrT = model.AttrT;
						entity.AttrU = model.AttrU;
						entity.AttrV = model.AttrV;
						entity.AttrW = model.AttrW;
						entity.AttrX = model.AttrX;
						entity.AttrY = model.AttrY;
						entity.AttrZ = model.AttrZ;
 
                        db.Flow_Form.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
